package com.plsjavaweb.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.dao.interfaces.SearchDAO;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.SearchPOJO;

public class SearchCore {
	
	static Logger log= Logger.getLogger(DAOFactory.class);
	static LoggingConstants logConst = new LoggingConstants();
	
public List<SearchPOJO> getSearchEmployee(String search) {
		
		log.info(logConst.entryMessage);

		boolean isInteger = validateInteger(search);

		StringBuilder validationMessage = new StringBuilder();		
		List<SearchPOJO> searchDB = new ArrayList<>();	
		
		DAOFactory daoFactory = new DAOFactory();
		SearchDAO searchDAO = daoFactory.getSearchDAO();
		System.out.println("isInteger: "+ isInteger);
		
		try {		
			if (isInteger == false) {				
				searchDB = searchDAO.validateEmployeeExists(search);
				
				
				if (searchDB.isEmpty() == true) {									
					List<SearchPOJO> searchVal = searchDAO.validateEmployeeResourceProject(search);

					if (searchVal.isEmpty() == true) {
						
						SearchPOJO searchItem = new SearchPOJO();
						
						validationMessage.append("No Employee record. ");
						searchItem.setValid(false);
						searchItem.setValidationMessage(validationMessage.toString());					
						searchDB.add(searchItem);
						
					} else if (searchVal.isEmpty() == false) {						
						for (SearchPOJO searchPOJO : searchVal) {
							searchPOJO.setValid(true);
							searchPOJO.setValidationMessage("Found Employee record. ");
							searchDB.add(searchPOJO);
						}						
					}
				} else if (searchDB.isEmpty() == true) {					
					for (int i = 0; i < searchDB.size(); i++) {
						searchDB.get(i).setValid(true);
						searchDB.get(i).setValidationMessage("Found Employee record. ");
					}					
				}				
			} else if (isInteger == true) {				
				searchDB = searchDAO.validateEmployeeId(Integer.parseInt(search));
				
				if (searchDB.isEmpty() == true){

					SearchPOJO searchItem = new SearchPOJO();
					
					validationMessage.append("No Employee record. ");
					searchItem.setValid(false);
					searchItem.setValidationMessage(validationMessage.toString());					
					searchDB.add(searchItem);
					
				} else if (searchDB.isEmpty() == false) {
					for (int i = 0; i < searchDB.size(); i++) {
						searchDB.get(i).setValid(true);
						searchDB.get(i).setValidationMessage("Found Employee record. ");
					}
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			log.info(logConst.errorMessage);
		}
		
		log.info(logConst.exitMessage);
		return searchDB;		
	}
	
	public boolean validateInteger(String search) {	
		log.info(logConst.entryMessage );
		int parsedInt;

		try {			
			parsedInt = Integer.parseInt(search);			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			//log.info("parsedInt: " + parsedInt );			
			return false;
		}		
	}
}

